//
//  SFPNutrientIntakeController.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import UIKit
import BRPickerView
class SFPNutrientIntakeController: SFPBaseViewController,UITextFieldDelegate {
    @IBOutlet var weightLasLabel: UILabel!
    @IBOutlet var weightPreLabel: UILabel!
    @IBOutlet var tableView: UITableView!
    @IBOutlet var carLabel: UILabel!
    @IBOutlet var proLabel: UILabel!
    @IBOutlet var fatLabel: UILabel!
    @IBOutlet var bgView: UIView!
    @IBOutlet var dateLabel: UILabel!
    @IBOutlet var carCompleted: UIImageView!
    @IBOutlet var proCompleted: UIImageView!
    @IBOutlet var fatCompleted: UIImageView!
    @IBOutlet var carWidth: NSLayoutConstraint!
    @IBOutlet var proWidth: NSLayoutConstraint!
    @IBOutlet var fatWidth: NSLayoutConstraint!
    var currentDate = Date().toString(format: "yyyy.MM.dd")
    lazy var recordViewModel: SFPBodyRecordViewModel = {
        return SFPBodyRecordViewModel()
    }()
    lazy var viewModel: SFPNutrientIntakeViewModel = {
        return SFPNutrientIntakeViewModel()
    }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getPageData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setTableView()
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.bgView.addRoundCorners(topLeftRadius: 35, topRightRadius: 35, bottomLeftRadius: 0, bottomRightRadius: 0, borderWidth: 0, borderColor: .red)
    }
    func setTableView(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "SFPNutrientIntakeListCell", bundle: .main), forCellReuseIdentifier: "SFPNutrientIntakeListCell")
    }
    func getPageData(){
        self.recordViewModel.loadData( onSuccess: {  models in
            let model = models.first
            self.weightPreLabel.text = model?.weight.components(separatedBy: ".").first
            self.weightLasLabel.text = "." + (model?.weight.components(separatedBy: ".").last)! + "kg"
        })
        self.viewModel.loadData(currentDate, onSuccess: {[self] models in
            self.carLabel.text = String(format: "%ldg/710g", self.viewModel.carcount)
            self.proLabel.text = String(format: "%ldg/300g", self.viewModel.procount)
            self.fatLabel.text = String(format: "%ldg/100g", self.viewModel.fatcount)
            self.carCompleted.isHidden = self.viewModel.carcount > 709 ? false : true
            self.proCompleted.isHidden = self.viewModel.procount > 299 ? false : true
            self.fatCompleted.isHidden = self.viewModel.fatcount > 99 ? false : true
            self.carWidth.constant = calculateWidth(count: self.viewModel.carcount, threshold: 709)
            self.proWidth.constant = calculateWidth(count: self.viewModel.procount, threshold: 299)
            self.fatWidth.constant = calculateWidth(count: self.viewModel.fatcount, threshold: 99)
            self.tableView.reloadData()
        })
    }
    func calculateWidth(count: Int, threshold: Int) -> CGFloat {
        guard count > 0 else {
            return 0 // 防止除以零错误
        }
        let baseWidth: CGFloat = 194.0
        let additionalConstant: Double = 15

        if count > threshold {
            return baseWidth
        } else {
            return baseWidth * CGFloat((Double(count) + additionalConstant) / Double(threshold))
        }
    }
    @IBAction func gotoNutritionalRecord(_ sender: Any) {
        self.navigationController?.pushViewController(SFPNutritionalRecordController(), animated: true)
    }
    @IBAction func gotoBodyRecord(_ sender: Any) {
        self.navigationController?.pushViewController(SFPBodyRecordController(), animated: true)
    }
    @IBAction func gotoSelectDate(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRDatePickerView(pickerMode: .YMD)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.resultBlock = { selectDate,selectValue in
            self.currentDate = (selectDate?.toString(format: "yyyy.MM.dd"))!
            if self.currentDate.isToday(format: "yyyy.MM.dd"){
                self.dateLabel.text = "Today"
            }else{
                self.dateLabel.text = String(format: "%@", self.currentDate)
            }
            self.getPageData()
        }
        pickerView.show()
    }
}
extension SFPNutrientIntakeController:UITableViewDataSource,UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:SFPNutrientIntakeListCell = tableView.dequeueReusableCell(withIdentifier: "SFPNutrientIntakeListCell") as! SFPNutrientIntakeListCell
        cell.model = self.viewModel.dataSource[indexPath.row]
        return cell
    }
}
